package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.dao.DeliveryOrderDo;
import com.ruoyi.system.dao.WarehousingEntryDo;
import com.ruoyi.system.domain.DeliveryOrderInfo;
import com.ruoyi.system.domain.WarehousingEntryInfo;
import com.ruoyi.system.mapper.DeliveryOrderInfoMapper;
import com.ruoyi.system.mapper.WarehousingEntryInfoMapper;
import com.ruoyi.system.request.DeliveryOrderListRequest;
import com.ruoyi.system.response.WarehousingEntryInfoDetailList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DeliveryOrderMapper;
import com.ruoyi.system.domain.DeliveryOrder;
import com.ruoyi.system.service.IDeliveryOrderService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.CollectionUtils;

/**
 * 出库单Service业务层处理
 *
 * @author tanshen
 * @date 2023-08-21
 */
@Service
public class DeliveryOrderServiceImpl implements IDeliveryOrderService {
    @Autowired
    private DeliveryOrderMapper deliveryOrderMapper;
    @Autowired
    private DeliveryOrderInfoMapper deliveryOrderInfoMapper;

    /**
     * 查询出库单
     *
     * @param id 出库单主键
     * @return 出库单
     */
    @Override
    public DeliveryOrder selectDeliveryOrderById(Long id) {
        return deliveryOrderMapper.selectDeliveryOrderById(id);
    }

    /**
     * 查询出库单列表
     *
     * @param request 出库单
     * @return 出库单
     */
    @Override
    public List<DeliveryOrder> selectDeliveryOrderList(DeliveryOrderListRequest request) {
        String materialId = request.getMaterialId();
        DeliveryOrderDo deliveryOrderDo = new DeliveryOrderDo();
        BeanUtils.copyBeanProp(deliveryOrderDo, request);

        List<String> deliveryOrderIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(materialId)) {
            // 查询物料在哪些入库单里面
            DeliveryOrderInfo deliveryOrderInfo = new DeliveryOrderInfo();
            deliveryOrderInfo.setMaterialId(materialId);
            List<DeliveryOrderInfo> deliveryOrderInfos = deliveryOrderInfoMapper.selectDeliveryOrderInfoList(deliveryOrderInfo);
            if (CollectionUtils.isEmpty(deliveryOrderInfos)) {
                return new ArrayList<>();
            }
            deliveryOrderIds = deliveryOrderInfos.stream().map(DeliveryOrderInfo::getOrderId).distinct().collect(Collectors.toList());
        }
        deliveryOrderDo.setDeliveryOrderIds(deliveryOrderIds);
        return deliveryOrderMapper.selectDeliveryOrderList(deliveryOrderDo);
    }

    /**
     * 新增出库单
     *
     * @param deliveryOrder 出库单
     * @return 结果
     */
    @Override
    public int insertDeliveryOrder(DeliveryOrder deliveryOrder) {
        return deliveryOrderMapper.insertDeliveryOrder(deliveryOrder);
    }

    /**
     * 修改出库单
     *
     * @param deliveryOrder 出库单
     * @return 结果
     */
    @Override
    public int updateDeliveryOrder(DeliveryOrder deliveryOrder) {
        return deliveryOrderMapper.updateDeliveryOrder(deliveryOrder);
    }

    /**
     * 批量删除出库单
     *
     * @param ids 需要删除的出库单主键
     * @return 结果
     */
    @Override
    public int deleteDeliveryOrderByIds(String ids) {
        return deliveryOrderMapper.deleteDeliveryOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除出库单信息
     *
     * @param id 出库单主键
     * @return 结果
     */
    @Override
    public int deleteDeliveryOrderById(Long id) {
        return deliveryOrderMapper.deleteDeliveryOrderById(id);
    }
}
